/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.validator;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.appmoblie.RefundTktDto;
import com.ceair.flight.refund.location.dt.enums.RefundTktNature;

import java.math.BigDecimal;

/**
 *
 * @author Colin_You
 */
abstract class BaseValidator implements IValidator {

    @Override
    public boolean validate(RefundApplyDto refundApply) {
        return checkRefundAmount(refundApply);
    }

    boolean checkRefundAmount(RefundApplyDto refundApply) {
        BigDecimal refundAmt = refundApply.getRefundAmount();
        BigDecimal tktRefundAmt = this.getTktRefundAmtIncludeTax(refundApply);
        return refundAmt.compareTo(tktRefundAmt) == 0;
    }

    private BigDecimal getTktRefundAmtIncludeTax(RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        BigDecimal tktRefundAmt = refundTkt.getRefundAmt();
        BigDecimal refundTax = refundTkt.getRefundTax();
        tktRefundAmt = tktRefundAmt.add(refundTax);
        tktRefundAmt = tktRefundAmt.add(getFaceGap(refundApply));
        tktRefundAmt = tktRefundAmt.add(getTaxGap(refundApply));
        return tktRefundAmt;
    }

    BigDecimal getFaceGap(RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        String tktNature = refundTkt.getTktNature();
        if (RefundTktNature.RESCHEDULE == RefundTktNature.getByCode(tktNature)) {
            BigDecimal faceGap = refundTkt.getRescheduleGap();
            if (faceGap != null) {
                return faceGap;
            }
        }
        return BigDecimal.ZERO;
    }

    BigDecimal getTaxGap(RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        String tktNature = refundTkt.getTktNature();
        if (RefundTktNature.RESCHEDULE == RefundTktNature.getByCode(tktNature)) {
            BigDecimal taxGap = refundTkt.getRescheduleTax();
            if (taxGap != null) {
                return taxGap;
            }
        }
        return BigDecimal.ZERO;
    }

    BigDecimal getPromoteAmt(RefundApplyDto refundApply) {
        RefundTktDto refundTkt = refundApply.getRefundTkt();
        Long points = refundTkt.getTktPoints();
        BigDecimal promoteAmt = refundTkt.getPromoteAmt();
        return (points != null && points > 0) || promoteAmt == null ? BigDecimal.ZERO : promoteAmt;
    }

}
